package main

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
	cloudrtc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cloudrtc/v2/model"
)

var ak = "<YOUR AK>"
var sk = "<YOUR SK>"
var endpoint = "<CloudRTC URL>"
var projectID = "<YOUR projectID>"

func createClient() (client *cloudrtc.CloudRTCClient) {
	auth := basic.NewCredentialsBuilder().
		WithProjectId(projectID).
		WithAk(ak).
		WithSk(sk).
		Build()

	client = cloudrtc.NewCloudRTCClient(
		cloudrtc.CloudRTCClientBuilder().
			WithRegion(region.NewRegion("<YOUR REGION>", endpoint)).
			WithCredential(auth).
			Build())
	return
}

func createRecordRuleReq() *model.RecordRuleReq {
	object := "/record"
	var ecordMaxDurationToMergeFile int32 = 120
	var recordPrefix string = "{publish_domain}/{record_format}/{stream}/{file_start_time}"
	var recordSliceDuration int32 = 60

	return &model.RecordRuleReq{
		ObsAddr: &model.RecordObsFileAddr{
			Location:  model.GetRecordObsFileAddrLocationEnum().CN_NORTH_4,
			ProjectId: "123123",
			Bucket:    "123123",
			Object:    &object,
		},
		RecordFormats: []model.RecordRuleReqRecordFormats{
			model.GetRecordRuleReqRecordFormatsEnum().HLS,
			model.GetRecordRuleReqRecordFormatsEnum().MP4,
		},
		HlsConfig: &model.HlsRecordConfig{
			RecordCycle:                  300,
			RecordPrefix:                 &recordPrefix,
			RecordSliceDuration:          &recordSliceDuration,
			RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
		},
		Mp4Config: &model.Mp4RecordConfig{
			RecordCycle:                  300,
			RecordPrefix:                 &recordPrefix,
			RecordMaxDurationToMergeFile: &ecordMaxDurationToMergeFile,
		},
	}
}

func createRecordRule() {
	client := createClient()

	request := &model.CreateRecordRuleRequest{
		AppId: "<APPID>",
		Body:  createRecordRuleReq(),
	}
	response, err := client.CreateRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func getRecordRules() {
	client := createClient()

	request := &model.ListRecordRulesRequest{
		AppId: "<APPID>",
	}
	response, err := client.ListRecordRules(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func updateRecordRule() {
	client := createClient()

	request := &model.UpdateRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
		Body:   createRecordRuleReq(),
	}
	response, err := client.UpdateRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func getRecordRule() {
	client := createClient()

	request := &model.ShowRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
	}
	response, err := client.ShowRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func deleteRecordRule() {
	client := createClient()

	request := &model.DeleteRecordRuleRequest{
		AppId:  "<APPID>",
		RuleId: "<RuleID>",
	}
	response, err := client.DeleteRecordRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func UpdateRecordCallback() {
	client := createClient()

	authKey := "<AuthKey>"
	request := &model.UpdateRecordCallbackRequest{
		AppId: "<APPID>",
		Body: &model.AppCallbackUrlReq{
			Url:     "<Your URL>",
			AuthKey: &authKey,
		},
	}
	response, err := client.UpdateRecordCallback(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

func CreateIndividualStreamJob() {
	client := createClient()

	isRecordAudio := true
	var MaxTime int32 = 60
	// CAMERASTREAM or SCREENSTREAM
	videoType := model.GetIndividualStreamJobReqVideoTypeEnum().CAMERASTREAM
	// LD, SD, HD, or FHD
	streamType := model.GetIndividualStreamJobReqSelectStreamTypeEnum().HD
	request := &model.CreateIndividualStreamJobRequest{
		AppId: "<APPID>",
		Body: &model.IndividualStreamJobReq{
			RoomId:           "<RoomID>",
			UserId:           "<UserID>",
			IsRecordAudio:    &isRecordAudio,
			VideoType:        &videoType,
			SelectStreamType: &streamType,
			MaxIdleTime:      &MaxTime,
			RecordParam: &model.RecordParam{
				RecordRuleId: "<RecordRuleId>",
			},
		},
	}
	response, err := client.CreateIndividualStreamJob(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}
